#!/usr/bin/env php
<?

require dirname(__FILE__)."/../config/environment.php";

$type = $_SERVER['argv'][1];
$name = $_SERVER['argv'][2];

switch ($type)
{
	case 'model':
		$file = fopen(dirname(__FILE__)."/../app/models/{$name}_model.php", 'w+');
		fwrite($file, "<?\n\nclass {$name}_model extends model\n{\n}\n\n?>");
		fclose($file);
		break;

	case 'controller':
		$controller_name = str_replace('/', '_', $name);

		if (preg_match('/\//', $name)) { 
			$path = explode('/', $name);
			array_pop($path);
			$path = implode('/', $path);
			exec("mkdir -p ".dirname(__FILE__)."/../app/controllers/{$path}");
		}

		$file = fopen(dirname(__FILE__)."/../app/controllers/{$name}_controller.php", 'w+');

		touch(dirname(__FILE__)."/../app/helpers/{$controller_name}_helper.php", 'w+');
		exec("mkdir -p ".dirname(__FILE__)."/../app/views/{$name}");

		for ($i = 3; $i < count($_SERVER['argv']); $i++)
		{
			$view_name = $_SERVER['argv'][$i];

			$views .= "\n\tpublic function {$view_name}()\n\t{\n\t}\n";

			$view = fopen(dirname(__FILE__)."/../app/views/{$name}/{$view_name}.php", 'w+');
			fwrite($view, "<h1>{$view_name} View for {$controller_name}</h1>\n<p>You can find me in views/{$name}/{$view_name}.php</p>");
			fclose($view);
		}

		fwrite($file, "<?\n\nclass {$controller_name}_controller extends application_controller\n{{$views}}\n\n?>");
		fclose($file);
		break;
}

?>
